//#include <unistd.h>  // for F_ok
#include <glib.h>
#include <stdio.h> 
#include <unistd.h>

#include "config.h"
#include "sys_log.h"

static int wifi_opening_flag = 0;
static int keyboard_opening_flag = 0;

int start_soft_keyboard(void)
{
	SYS_LOG("start_soft_keyboard");
	int pid = 0;
	int ret = 0;
	int status = 0;



    system("sudo onboard &");

//	FILE *fp;
	
//	fp = popen("gnome-terminal --geometry=0x0+0+0 --hide-menubar -x sudo florence", "r");
//	fp = popen("gnome-terminal --geometry=0x0+0+0 --hide-menubar -x sudo onboard", "r");

//	fp = popen("onboard", "r");
//	pclose(fp);

	return pid;
}

int stop_soft_keyboard(void)
{

	int pid = 0;

	FILE *fp;
//	fp = popen("sudo killall florence", "r");
	fp = popen("sudo killall onboard", "r");

	pclose(fp);
	return pid;
}

int start_wifi_radar(void)
{
	SYS_LOG("start_wifi_radar");
	int pid = 0;
	int ret = 0;
	int status = 0;

	FILE *fp;
    system("sudo wicd-gtk &");
//	fp = popen("gnome-terminal --geometry=0x0+0+0 --hide-menubar -x sudo wicd-gtk", "r");
//	fp = popen("gnome-terminal --geometry=0x0+0+0 --hide-menubar -x sudo nm-connection-editor", "r");

//	pclose(fp);
	return pid;
}

int stop_wifi_radar(void)
{
	SYS_LOG("stop_wifi_radar");
	int pid = 0;

	FILE *fp;
	//fp = popen("sudo killall nm-connection-editor", "r");
	fp = popen("sudo killall wicd-client", "r");
	pclose(fp);

	return pid;
}
static gboolean sys_show_time_text_timeout(void *data)
{
	start_wifi_radar();

	return FALSE;
}

void show_key_wifi_in_new_win(GtkWidget* widget, gpointer data)
{
	SYS_LOG("show_key_wifi_in_new_win");
	//g_timeout_add(1000,(GSourceFunc)sys_show_time_text_timeout,NULL);

	if (0 == wifi_opening_flag) {
		start_wifi_radar();
		wifi_opening_flag=1;
	}else {
		stop_wifi_radar();
		wifi_opening_flag=0;
	}
	
	if (0 == keyboard_opening_flag) {
		start_soft_keyboard();
		keyboard_opening_flag = 1;
	} else {
		stop_soft_keyboard();
		keyboard_opening_flag = 0;
	}
	
}

